package ro.imc.training.UMS;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import ro.imc.training.Database.*;
public class UserInterface {
	private JFrame frame = new JFrame();
	JButton buttonStudent = new JButton("Student");
	JButton buttonProfesor = new JButton("Profesor");
	JButton buttonCourse = new JButton("Course");
	JButton buttonExam = new JButton("Exam");
	JButton buttonExit = new JButton("Exit");

	// private JPanel panel;

	public UserInterface() {
		frame.setLayout(new GridLayout(3, 2));
		addButtonsToGrid();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setSize(UIData.MAIN_FRAME_HEIGHT, UIData.MAIN_FRAME_WIDTH);
		frame.setResizable(false);
		frame.setVisible(true);

	}

	private void addButtonsToGrid() {

		frame.add(buttonStudent);
		frame.add(buttonProfesor);
		frame.add(buttonCourse);
		frame.add(buttonExam);
		frame.add(buttonExit);
		addActions();
	}

	ActionListener exitButtonAction = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);

		}

	};

	ActionListener studentButtonAction = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			JFrame studentFrame = new JFrame();
			JPanel panel = new JPanel();

			final JTextField firstNameText = new JTextField("First Name");
			final JTextField lastNameText = new JTextField("Last Name");
			final JTextField cnpText = new JTextField("CNP");
			final JTextField studentNoText = new JTextField("Student Number");

			JButton addButton = new JButton("Add");

			studentFrame.setLayout(null);
			// studentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			// studentFrame.setLocationRelativeTo(null);
			studentFrame.setSize(600, 300);
			studentFrame.setResizable(false);
			studentFrame.setVisible(true);

			firstNameText.setSize(200, 20);
			firstNameText.setLocation(10, 40);
			studentFrame.add(firstNameText);

			lastNameText.setSize(200, 20);
			lastNameText.setLocation(10, 80);
			studentFrame.add(lastNameText);

			cnpText.setSize(200, 20);
			cnpText.setLocation(10, 120);
			studentFrame.add(cnpText);

			studentNoText.setSize(200, 20);
			studentNoText.setLocation(10, 160);
			studentFrame.add(studentNoText);

			addButton.setSize(100, 30);
			addButton.setLocation(30, 200);
			studentFrame.add(addButton);

			ActionListener studentAddButtonAction = new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

					String firstName = firstNameText.getText();
					String lastName = lastNameText.getText();
					String cnp = cnpText.getText();
					String studentno = studentNoText.getText();

					Student student = new Student(firstName, lastName, cnp,
							studentno, "active");
//					System.out.println(student.firstName + "  "
//							+ student.lastName);
					
					StudentCrud sc = new StudentCrud();
					sc.insert((Object)student);
					
					ArrayList<Object> al = new ArrayList<Object>();
					al = (ArrayList<Object>)sc.read();
					ArrayList<Student> al2 = new ArrayList<Student>();
					for(Object o : al) {
						al2.add((Student)o);
					}
					
					
					for (int i = 0; i < al2.size(); i++){
						System.out.println("");
						System.out.format("%20s", al2.get(i).getLastName() + " | ");
						System.out.format("%20s", al2.get(i).getFirstName()+ " | ");
						System.out.format("%20s", al2.get(i).getCNP() + " | ");
						System.out.format("%20s", al2.get(i).getStudentNo()+ " | ");
						System.out.format("%20s", al2.get(i).getStatus() + " | ");
					}
//					System.out.println(al2.get(1).getFirstName() + al2.get(1).getLastName() );
					
					
					
					
					
				}
			};
			addButton.addActionListener(studentAddButtonAction);

		}
	};

	ActionListener profesorButtonAction = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("profesor button pressed");

		}
	};

	ActionListener courseButtonAction = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("course button pressed");

		}
	};

	ActionListener examButtonAction = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("exam button pressed");

		}
	};

	void addActions() {
		buttonExit.addActionListener(exitButtonAction);
		buttonStudent.addActionListener(studentButtonAction);
		buttonProfesor.addActionListener(profesorButtonAction);
		buttonCourse.addActionListener(courseButtonAction);
		buttonExam.addActionListener(examButtonAction);
	}

}
